package com.karson.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.exception.AsmrException;
import com.karson.common.model.TagInfo;
import com.karson.common.request.PageParamRequest;
import com.karson.common.request.TagRequest;
import com.karson.common.response.CommonResult;
import com.karson.common.response.TagResponse;
import com.karson.common.vo.TagVo;
import com.karson.service.dao.TagInfoMapper;
import com.karson.service.service.TagInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/tag")
@Tag(name = "标签管理接口")
public class TagInfoController {
    @Autowired
    private TagInfoService tagInfoService;
    @Autowired
    private TagInfoMapper tagInfoMapper;

    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<TagResponse>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest) {
        IPage<TagResponse> tagInfoIPage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<TagResponse> tagInfo =  tagInfoService.pageList(tagInfoIPage);
        return CommonResult.success(tagInfo);
    }
    @Operation(summary = "添加标签")
    @PostMapping("/add")
    public CommonResult<Void> addTag(@RequestBody TagInfo request) {
        TagInfo tag = tagInfoService.getOne(new QueryWrapper<TagInfo>().eq("name", request.getName()));
        if (tag != null) {
            return CommonResult.failed("重复添加标签");
        }
        request.setNum(0);
        boolean save = tagInfoService.saveTag(request);
        return save ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "更改标签")
    @PutMapping("/update")
    public CommonResult<Void> update (@RequestBody TagRequest request) {
        if (null == request.getId() || request.getId() <= 0) {
            throw new AsmrException("id 参数不合法");
        }
        TagInfo tag = tagInfoService.getOne(new QueryWrapper<TagInfo>().eq("name", request.getName()));
        if (tag != null) {
            return CommonResult.failed("已有该标签");
        }
        boolean update = tagInfoService.update(request);
        return update ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "删除标签")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete (@PathVariable Integer id) {
        // 1. 先查询是否存在，如果根本不存在这个标签，可以认为是参数错误，不需要执行删除操作
        TagInfo tagInfo = tagInfoService.getById(id);
        if (tagInfo == null) {
            return CommonResult.failed("标签不存在");
        }

        Boolean flag = tagInfoService.removeTag(id);

        return flag ? CommonResult.success("删除成功") : CommonResult.failed("删除主标签失败");
    }
    @Operation(summary = "获取标签列表")
    @GetMapping("/getTagList")
    public CommonResult<List<TagInfo>> getTagList(){
        List<TagInfo> list = tagInfoService.list();
        return CommonResult.success(list);
    }
    @Operation(summary = "根据标签id查找标签信息")
    @GetMapping("/getTagById/{id}")
    public CommonResult<TagInfo> getTagList(@PathVariable Integer id) {
        if (id != null && id < 0) {
            return CommonResult.failed("获取失败");
        }
        TagInfo tagInfo = tagInfoService.getById(id);
        return CommonResult.success(tagInfo);
    }
    @Operation(summary = "根据专辑Id查找标签信息")
    @GetMapping("/getTagByAlbumId/{id}")
    public CommonResult<List<TagVo>> getTagByAlbumId(@PathVariable Integer id) {
        if (id != null && id < 0) {
            return CommonResult.failed("获取失败");
        }
        List<TagVo> tagResponseList = tagInfoMapper.getTagByAlbumId(id);
        return CommonResult.success(tagResponseList);
    }
}
